package com.ogawa.chair7808.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.Constant.MqttConstant;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.cache.GlobalCache;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.utils.Activity7808Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.ref.WeakReference;

/**
 * Created by FHB on 2018/4/9.
 */
public class LayoutFunctionView7808 extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private ImageView mIvAdjust;
    private ImageView mIvFoodRoll;
    private ImageView mIvHotWaist;
    private ImageView mIvHotLeg;
    private ImageView mIvLed;
    private LinearLayout mLlAdjust;
    private LinearLayout mLlFoodRoll;
    private LinearLayout mLlMusic;
    private LinearLayout mLlHoot;
    private LinearLayout mLlLed;
    private MyHandler myHandler;

    public LayoutFunctionView7808(Context context) {
        super(context);
        mContext = context;
    }

    public LayoutFunctionView7808(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvAdjust = findViewById(R.id.layout_function_tv_zttj);
        mIvFoodRoll = findViewById(R.id.layout_function_tv_jdgl);
        mIvHotWaist = findViewById(R.id.layout_function_tv_ybwg);
        mIvHotLeg = findViewById(R.id.layout_function_tv_tbwg);
        mIvLed = findViewById(R.id.layout_function_tv_led);

        mIvHotLeg.setOnClickListener(this);
        mIvHotWaist.setOnClickListener(this);

        mIvAdjust.setOnClickListener(this);
        mIvFoodRoll.setOnClickListener(this);
        mIvLed.setOnClickListener(this);
        initBody();
    }


    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == View.VISIBLE) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        } else {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }

        }
    }

    @Subscribe
    public void receiveMassageDeviceChange(String message) {
        if (!MqttConstant.EVENT_BUS_KEY_RECEIVE_MESSAGE.equals(message)) {
            return;
        }
        try {
            initBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    int[] ybjrRes = {R.mipmap.ybjr, R.mipmap.ybjr1, R.mipmap.ybjr2, R.mipmap.ybjr3};
    int[] tbjrRes = {R.mipmap.tbjr, R.mipmap.tbjr1, R.mipmap.tbjr2, R.mipmap.tbjr3};
    int tbjrState = 0;
    int ybjrState = 0;

    private void initBody() {

        if (!TextUtils.isEmpty(GlobalCache.getDeviceStatusBean().getHeatKneeLevelRank())) {
            tbjrState = Integer.valueOf(GlobalCache.getDeviceStatusBean().getHeatKneeLevelRank());
        }
        if (!TextUtils.isEmpty(GlobalCache.getDeviceStatusBean().getHeatWaistLevelRank())) {
            ybjrState = Integer.valueOf(GlobalCache.getDeviceStatusBean().getHeatWaistLevelRank());
        }
        mIvHotWaist.setImageResource(ybjrRes[ybjrState]);
        mIvHotLeg.setImageResource(tbjrRes[tbjrState]);
//        mIvHotWaist.setSelected(
////                "1".equals(GlobalCache.getDeviceStatusBean().getHeatFullBody())
////                        ||"1".equals(GlobalCache.getDeviceStatusBean().getHeatFullBody())
//                        "1".equals(GlobalCache.getDeviceStatusBean().getHeatWaist())
//
//        );
////        mIvHotLeg.setSelected( "1".equals(GlobalCache.getDeviceStatusBean().getFootWheelIcon()));


        mIvFoodRoll.setSelected("1".equals(GlobalCache.getDeviceStatusBean().getFootWheelIcon()));
        mIvLed.setSelected("1".equals(GlobalCache.getDeviceStatusBean().getLedIcon()));
    }


    public void onDestory() {
    }


    private class MyHandler extends Handler {
        WeakReference<LayoutFunctionView7808> mLayoutFunctionViewWeakReference;

        MyHandler(LayoutFunctionView7808 mLayoutFunctionView7506) {
            mLayoutFunctionViewWeakReference = new WeakReference<>(mLayoutFunctionView7506);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutFunctionView7808 mLayoutFunctionView7506 = mLayoutFunctionViewWeakReference.get();

            if (mLayoutFunctionView7506 != null) {
                String data = (String) msg.obj;


            }
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.layout_function_tv_zttj) {
            Activity7808Utils.startAttitudeAdjustmentActivity(mContext);

        } else if (i == R.id.layout_function_tv_jdgl) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.JIAODGL, "3");

            }


        } else if (i == R.id.layout_function_tv_ybwg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                    DataManager.getDataManager().sendCommond(CommmandNum.HOTWAIST, "2");


            }


        } else if (i == R.id.layout_function_tv_tbwg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                 DataManager.getDataManager().sendCommond(CommmandNum.HOTLEG, "3");

            }
        } else if (i == R.id.layout_function_tv_led) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.LED, "12");
            }


        }

    }
}
